package com.woniuxy.hrp.web;

import com.woniuxy.hrp.entity.OrderUser;
import com.woniuxy.hrp.entity.Patient;
import com.woniuxy.hrp.service.OrderUserService;
import com.woniuxy.hrp.service.PatientService;
import com.woniuxy.hrp.unityhandler.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2022/8/30 15:35
 */
@Transactional
@CrossOrigin
@RestController
@RequestMapping("/api/v1/orderuser")
public class OrderUserController {
    @Autowired
    PatientService patientService;
    @Autowired
    OrderUserService orderUserService;
    /**
     * desc: 增加就诊人(每个患者的就诊人不超过五个,且默认的就诊人只有一个)
     * @Param: [orderUser]
     * @Return: [com.woniuxy.hrp.entity.OrderUser]
     * @Author: caojieke
     * @Date: 2022/8/30 15:41
     */
    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody OrderUser orderUser){
        if (orderUser.getName()==null || orderUser.getGender()==null || orderUser.getAge()==0 || orderUser.getPhone()==null){
            return new ResponseEntity("700","EmptyInfo");
        }
        orderUserService.add(orderUser);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 根据患者id查询所有就诊人信息
     * @param orderUser
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity getAll(@RequestBody OrderUser orderUser){
        List<OrderUser> orderUsers = orderUserService.selectAll(orderUser.getPatient().getId());
        return new ResponseEntity("200","ok",orderUsers);
    }

    /**
     * 根据就诊人id删除就诊人
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public ResponseEntity deleteById(int id){
        orderUserService.deleteById(id);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 根据就诊人信息动态修改就诊人信息
     * @param orderUser
     * @return
     */
    @PostMapping("/update")
    public ResponseEntity updateById( @RequestBody OrderUser orderUser){
        //判断是否要将状态设为默认
        //不是直接更改
        if(orderUser.getDef().equals("n")){
            orderUserService.updateById(orderUser);
            return ResponseEntity.SUCCESS;
            //是，先将其他就诊人状态改为非默认
        }else{
            OrderUser orderUser1 = orderUserService.getById(orderUser.getId());
            List<OrderUser> orderUsers = orderUserService.selectAll(orderUser1.getPatient().getId());
            if(orderUsers!=null){//判断是否有就诊人
                for(int i=0;i<orderUsers.size();i++){
                    OrderUser u=orderUsers.get(i);
                    u.setDef("n");
                    orderUserService.updateById(u);
                }
            }
            orderUserService.updateById(orderUser);
            return ResponseEntity.SUCCESS;
        }

    }


}
